/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.locator
{
	import com.autoswf.core.ILink;
	
	/**
	 * An object used by links to get a resource synchronously or asynchronously from a locator.
	 */
	public class ResourceRequest
	{
		protected var _resource:Class;
		protected var _handler:Function;
		protected var _result:Object;
		
		
		/**
		 * Constructor. 
		 * @param resource  The class definition of the resource being requested.
		 * @param handler  The handler to invoke on resource acquisition.
		 */
		public function ResourceRequest(resource:Class, handler:Function = null)
		{
			_resource = resource;
			_handler = handler;
		}
		
		
		/**
		 * The class definition of the resource being requested.
		 */
		public function get resource():Class
		{
			return _resource;
		}
		
		
		/**
		 * The handler to invoke on resource acquisition.
		 */
		public function get handler():Function
		{
			return _handler;
		}
		
		
		/**
		 * The result of the resource request.
		 */
		public function get result():Object
		{
			return _result;
		}
		
		
		/**
		 * Used to set the requested resource reference.
		 */
		public function set result(value:Object):void
		{
			_result = value;
		}
		
		
		/**
		 * Convenience method for synchronously getting a resource and returning it.
		 * @return  The resource, if it exists.
		 */
		public function from(link:ILink):*
		{
			link.sendUp(this); /* If the resource exists, "_result" will now reference it. */
			return _result;
		}
		
	}
}